﻿package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import server.ComActionInterface;
import server.PersonInterface;

import client.Configuration;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;


public class ComEatGUI extends JDialog {

	private JPanel contentPane;
	Person p = new Person();
	int pid=p.getPID();


	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ComEatGUI frame = new ComEatGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public ComEatGUI() {
		setTitle("Eat");
		setModalityType(ModalityType.APPLICATION_MODAL);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("\u81EA\u5DF1\u505A\u996D");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Configuration config = new Configuration();
				String rmiAddress=config.getRmiAddress();
				ComActionInterface comAction;
					
					try {
						comAction = (ComActionInterface) Naming.lookup(rmiAddress+"ComActionList");
						int h = comAction.getHP("cook");
						int ha = comAction.getHap("cook"); 
						int h1 = comAction.getHP("eat");
						int ha1 = comAction.getHap("eat");
						System.out.print(h);
						PersonInterface person = (PersonInterface) Naming.lookup(rmiAddress+"Person");
						boolean z = person.hpChanges(pid, h, '-');
						boolean y = person.hapChanges(pid, ha, '-');
						boolean z1 = person.hpChanges(pid, h1, '+');
						boolean y1 = person.hapChanges(pid, ha1, '+');
						if(z&&y&&z1&&y1)
						    JOptionPane.showMessageDialog(null, "您做饭减少了1点HP和1点Hap。\n您吃饭增加了2点HP和2点Hap。");
						else
							JOptionPane.showMessageDialog(null, "您的HP和Hap都不足");
					} catch (MalformedURLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NotBoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} 
					
			}
		});
		btnNewButton.setBounds(74, 42, 271, 23);
		contentPane.add(btnNewButton);
		
		JButton button = new JButton("\u53EB\u5916\u5356");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				ComTakeoutGUI ctg = new ComTakeoutGUI();
				ctg.setLocationRelativeTo(null);
				ctg.setVisible(true);
				
			}
		});
		button.setBounds(74, 115, 271, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("\u8FD4\u56DE");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				ComHouseGUI chg = new ComHouseGUI();
				chg.setLocationRelativeTo(null);
				chg.setVisible(true);
				
			}
		});
		button_1.setBounds(301, 205, 81, 23);
		contentPane.add(button_1);
	}

}
